package jp.co.piratealliance.pirateAllianceWeb.web.action.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import jp.co.piratealliance.pirateAllianceWeb.common.base.BaseAction;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.UserInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.task.ScheduleAndEventInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.service.task.TaskService;


/**
 * copyright pirate alliance team 2014 日程登陆
 *
 * @author panxuejuan
 */

public class EventManagerWebAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String viewFlag;
	
	private TaskService taskService;

	private List<ScheduleAndEventInfoDto> eventList;
	
	private String start_time;

	private String end_time;

	private String title;

	private String place;

	private String memo;

	private String day;
	
	private String eventIdForModify="";
	
	private String scheduleIdForModify="";
	
	private String orderId;
	
	//日程页面初期化
	public String eventManagerInit()
	{
		UserInfoDto userDto=(UserInfoDto) ActionContext.getContext().getSession().get("user");
		if(userDto!=null){
			orderId = ServletActionContext.getRequest().getParameter("orderId");
			eventList = taskService.getEventListByUserId(orderId);
			return SUCCESS;
		}
		return INPUT;
	}

	//日程追加
	public String saveEventManagerInfo()
	{
		ScheduleAndEventInfoDto eventInfo=new ScheduleAndEventInfoDto();
		//String orderId="2014081200000002";
		eventInfo.setDay(day);
		eventInfo.setStart_time(day+" "+start_time);
		eventInfo.setEnd_time(day+" "+end_time);
		eventInfo.setTitle(title);
		eventInfo.setPlace(place);
		eventInfo.setMemo(memo);
		eventInfo.setOrder_id(orderId);
		if("".equals(eventIdForModify)){
			taskService.insertEventInfo(eventInfo);//新增日程信息
		}else{
			eventInfo.setEvent_id(eventIdForModify);
			eventInfo.setSchedule_id(scheduleIdForModify);
			boolean successFlg=taskService.updateEventInfo(eventInfo);//更新日程信息
			if(successFlg){
				eventIdForModify="";
				scheduleIdForModify="";
			}
		}
		eventManagerInit();
		return SUCCESS;
	}

	//查看日程详情
	public String viewEventManagerInfo()
	{
		String scheduleId = ServletActionContext.getRequest().getParameter("scheduleId");
		String eventId = ServletActionContext.getRequest().getParameter("eventId");
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("order_id", orderId);
		param.put("schedule_id", scheduleId);
		param.put("event_id", eventId);
		eventIdForModify = eventId;
		scheduleIdForModify=scheduleId;
		List<ScheduleAndEventInfoDto> eidtEventList = taskService.getEventListByEventId(param);	
		for(ScheduleAndEventInfoDto eventInfo:eidtEventList){
			this.day=eventInfo.getDay();
			this.start_time=eventInfo.getStart_time();
			this.end_time=eventInfo.getEnd_time();
			this.title=eventInfo.getTitle();
			this.place=eventInfo.getPlace();
			this.memo=eventInfo.getMemo();
		}
		eventManagerInit();
		viewFlag="1";
		return SUCCESS;
	}

	//删除日程信息
	public String delectEventManagerInfo()
	{
		String scheduleId = ServletActionContext.getRequest().getParameter("scheduleId");
		String eventId = ServletActionContext.getRequest().getParameter("eventId");
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("order_id", orderId);
		param.put("schedule_id", scheduleId);
		param.put("event_id", eventId);
		taskService.deleteEventListByEventId(param);
		eventManagerInit();
		return SUCCESS;
	}
	//修改日程信息
	public String modifyEventManagerInfo()
	{
		String scheduleId = ServletActionContext.getRequest().getParameter("scheduleId");
		String eventId = ServletActionContext.getRequest().getParameter("eventId");
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("order_id", orderId);
		param.put("schedule_id", scheduleId);
		param.put("event_id", eventId);
		eventIdForModify = eventId;
		scheduleIdForModify=scheduleId;
		List<ScheduleAndEventInfoDto> eidtEventList = taskService.getEventListByEventId(param);	
		for(ScheduleAndEventInfoDto eventInfo:eidtEventList){
			this.day=eventInfo.getDay();
			this.start_time=eventInfo.getStart_time();
			this.end_time=eventInfo.getEnd_time();
			this.title=eventInfo.getTitle();
			this.place=eventInfo.getPlace();
			this.memo=eventInfo.getMemo();
		}
		eventManagerInit();
		return SUCCESS;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public List<ScheduleAndEventInfoDto> getEventList() {
		return eventList;
	}

	public void setEventList(List<ScheduleAndEventInfoDto> eventList) {
		this.eventList = eventList;
	}
	
	public String getStart_time() {
		return start_time;
	}

	public void setStart_time(String start_time) {
		this.start_time = start_time;
	}

	public String getEnd_time() {
		return end_time;
	}

	public void setEnd_time(String end_time) {
		this.end_time = end_time;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	public String getEventIdForModify() {
		return eventIdForModify;
	}

	public void setEventIdForModify(String eventIdForModify) {
		this.eventIdForModify = eventIdForModify;
	}

	public String getScheduleIdForModify() {
		return scheduleIdForModify;
	}

	public void setScheduleIdForModify(String scheduleIdForModify) {
		this.scheduleIdForModify = scheduleIdForModify;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	
	public String getViewFlag() {
		return viewFlag;
	}

	public void setViewFlag(String viewFlag) {
		this.viewFlag = viewFlag;
	}
	
}
